/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.activity;


import java.util.List;

import net.bezrabotnih.cabinet.client.place.ProfileEditPlace;
import net.bezrabotnih.cabinet.client.place.ProfilePlace;
import net.bezrabotnih.cabinet.client.request.CabinetRequestFactory;
import net.bezrabotnih.cabinet.client.ui.ProfileView;
import net.bezrabotnih.cabinet.client.ui.ProfileViewImpl;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.ServerFailure;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.impl.PopupImpl;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class ProfileActivity extends AbstractActivity implements ProfileView.Presenter {

	/**
	 * Sample property.
	 */
	private String name;
	
	private CabinetRequestFactory requests;
	
	private PlaceController placeController;
	
	private UserAccountProxy currentUserAccount;
	
	private ProfileView view;

	public ProfileActivity(ProfilePlace place,CabinetRequestFactory requests,PlaceController placeController) {
		this.name = place.getName();
		this.requests=requests;
		this.placeController=placeController;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = ProfileViewImpl.instance();
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		requests.cabinetUserAccountRequest().currentUserAccount().fire(new Receiver<UserAccountProxy>() {
			@Override
			public void onSuccess(UserAccountProxy userAccountProxy) {
				currentUserAccount=userAccountProxy;
				initData(currentUserAccount);
			}
			
		});
	}
	
	@SuppressWarnings("deprecation")
	private void initData(UserAccountProxy userAccountProxy){
		if (view!=null){
			
			view.setCity("Санкт-Петербург");
			if (userAccountProxy.getAge()!=null) view.setAge(userAccountProxy.getAge());
			if (userAccountProxy.getName()!=null) view.setName(userAccountProxy.getName()+", "+(userAccountProxy.getAccountType()==0?"исполнитель":"работодатель"));
			if (userAccountProxy.getPhoneNumber()!=null) view.setPhoneNumber(userAccountProxy.getPhoneNumber());
			if (userAccountProxy.getSex()!=null)  view.setSex(userAccountProxy.getSex()==0);
			List<String> ids=userAccountProxy.getJobTypeIds();
			if (userAccountProxy.getAccountType()==0){
				requests.cabinetJobTypeRequest().findJobTypes(ids).fire(new Receiver<List<JobTypeProxy>>() {
				@Override
				public void onSuccess(List<JobTypeProxy> jobTypeProxies) {
					view.setJobTypes(jobTypeProxies);
					
				}
			});
			}else{
				view.setJobTypes(null);
			}
			
			
		}
	}

	@Override
	public String mayStop() {
		return null;
	}

	/**
	 * @see ProfileView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		// TODO
	}

	@Override
	public void editClicked() {
		placeController.goTo(new ProfileEditPlace());
		
	}
}
